<h1>Locatii oferta</h1>

<div id="map_canvas" style="margin: 10px 10px 10px 0px; border: 1px solid #303030; width:340px; height:300px"></div>

<form action="<?php echo url_for('offer/locations?offer_id='.$offer->getId()) ?>" method="post" enctype="multipart/form-data">
  <table>
    <tfoot>
      <tr>
        <td colspan="2">
          <?php echo $form->renderHiddenFields(false) ?>
          <input type="submit" name="next" value="Save" />
          <input type="submit" name="next" value="Save and add Images" />
          <input type="submit" name="next" value="Save and go to Offer" />
          <br />
          &nbsp;<a href="<?php echo url_for('offer/index') ?>">Back to list</a>
          &nbsp;<a href="<?php echo url_for('offer/edit?id='.$offer->getId()) ?>">Offer</a>
          &nbsp;<a href="<?php echo url_for('offer/images?offer_id='.$offer->getId()) ?>">Images</a>
        </td>
      </tr>
    </tfoot>
    <tbody>
      <?php echo $form->renderGlobalErrors() ?>
      <tr>
        <th><?php echo $form['address']->renderLabel() ?></th>
        <td>
          <?php echo $form['address']->renderError() ?>
          <?php echo $form['address'] ?>
          <input type="button" onclick="codeAddress();" value="Search Address on Map" />
        </td>
      </tr>
      <tr>
        <th><?php echo $form['latitude']->renderLabel() ?></th>
        <td>
          <?php echo $form['latitude']->renderError() ?>
          <?php echo $form['latitude'] ?>
        </td>
      </tr>
      <tr>
        <th><?php echo $form['longitude']->renderLabel() ?></th>
        <td>
          <?php echo $form['longitude']->renderError() ?>
          <?php echo $form['longitude'] ?>
        </td>
      </tr>
    </tbody>
  </table>
</form>

<?php foreach($offer_locations as $offer_location): ?>
  (<?php echo $offer_location->getLatitude() ?>, <?php echo $offer_location->getlongitude() ?>)
  <a href="<?php echo url_for('offer/deleteLocation?id='.$offer_location->getId()) ?>">Sterge</a>
<?php endforeach; ?>

<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
if(document.getElementById("offer_location_latitude").value && document.getElementById("offer_location_longitude").value)
{
  var latlng = new google.maps.LatLng(document.getElementById("offer_location_latitude").value, document.getElementById("offer_location_longitude").value);
}
else
{
  var latlng = new google.maps.LatLng(44.437, 26.097);
}
var myOptions = {
  zoom: 8,
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var marker = new google.maps.Marker({map: map, draggable: true});
google.maps.event.addListener(map, 'click', function(event) {
    placeMarker(event.latLng, marker);
});
google.maps.event.addListener(marker, 'mouseup', function(event) {
    codeLatLng(marker.getPosition());
    latlng = marker.getPosition();
 });
 
var codeAddress = function()
{
    var address = document.getElementById("offer_location_address").value;
    if(!address) return;

    geocoder.geocode({'address': address}, function(results, status) {
        if(status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            marker.setPosition(results[0].geometry.location);
            document.getElementById("offer_location_address").value = results[0].formatted_address;
            document.getElementById("offer_location_latitude").value = results[0].geometry.location.lat();
            document.getElementById("offer_location_longitude").value = results[0].geometry.location.lng();
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

var codeLatLng = function(latlng) {
    geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
                marker.setPosition(latlng);
                document.getElementById("offer_location_address").value = results[0].formatted_address;
                document.getElementById("offer_location_latitude").value = latlng.lat();
                document.getElementById("offer_location_longitude").value = latlng.lng();
            }
        } else {
            alert("Geocoder failed due to: " + status);
        }
    });
}

placeMarker = function(location, marker)
{
    var clickedLocation = new google.maps.LatLng(location);
    marker.setPosition(location);
    codeLatLng(location);
}

if(document.getElementById("offer_location_latitude").value && document.getElementById("offer_location_longitude").value)
{
  codeLatLng(new google.maps.LatLng(document.getElementById("offer_location_latitude").value, document.getElementById("offer_location_longitude").value));
}
</script>